//
//  WebViewController.m
//  UI_webView试验
//
//  Created by 孙宇航 on 15/1/25.
//  Copyright (c) 2015年 孙宇航. All rights reserved.
//

#import "WebViewController.h"
#import "UIImage+GaussianBlur.h"
#import "AAA.h"
#import "WebModel.h"



#define DEFAULTS ([NSUserDefaults standardUserDefaults])

@interface WebViewController ()<UIWebViewDelegate>
@property (strong, nonatomic)AAA *indicator;
@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.webView = [[UIWebView alloc ] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.webView.delegate = self;
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.yueduziji.com/v/%@",_myid]]];
    // 真美
    [_webView loadRequest:request];
    [self.view addSubview:_webView];
    [self downView];
    NSString * font1 = [NSString stringWithFormat:@"%d", 85];
    [DEFAULTS setObject:font1 forKey:@"numOfFont"];
    
    // 右侧按钮
     self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xiala1"] style:UIBarButtonItemStyleDone target:self action:@selector(startButton)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"fanhui.png"] style:UIBarButtonItemStyleDone target:self action:@selector(buttonAction:)];
    
    
//    
//    NSMutableArray * newArray = [WebData seachAlldouber];
//    int a = 1;
//    for(WebModel * model in newArray){
//        
//        if ([_myid isEqualToString:model.myId]) {
//            a++;
//        } else {
//            
//        }
//    }
//    if (a == 2) {
//        
//        
//        [self.favorite setBackgroundImage:[UIImage imageNamed:@"yishoucang"] forState:UIControlStateNormal];
//        [_favorite addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    }else{
//        [self.favorite setBackgroundImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
//        [_favorite addTarget:self action:@selector(ButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//    }
    
}

- (void)downView
{
    // 字体界面
    self.showView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -135, self.view.frame.size.width, 150)];
    _showView.image = [UIImage imageWithBlurImageNamed:[UIImage imageNamed:@"show"] intputRadius:3.3];
    _showView.alpha = 1;
    _showView.layer.cornerRadius = 10;
    
    UIButton *small = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 90, self.view.frame.size.height *0.15, 25, 25)];
    small.titleLabel.text= @"小";
    [small setBackgroundImage:[UIImage imageNamed:@"zitixiao"] forState:UIControlStateNormal];
    [small addTarget:self action:@selector(reduceFontButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [_showView addSubview:small];
    
    UIButton *big = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 - 90, self.view.frame.size.height * 0.047, 25, 25)];
    [big setBackgroundImage:[UIImage imageNamed:@"zitida"] forState:UIControlStateNormal];
    [big addTarget:self action:@selector(addFontButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [_showView addSubview:big];
    //    small.layer.contentsRect = ;
    [_showView setUserInteractionEnabled:YES];
    [self.webView addSubview:_showView];
    
    //收藏按钮
    self.favorite = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 + 50,  self.view.frame.size.height * 0.047, 25, 25)];
    [_favorite setBackgroundImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
    
    [_showView addSubview:_favorite];
    
    //分享按钮
    self.share= [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2 + 50,  self.view.frame.size.height *0.15, 25, 25)];
    [_share setBackgroundImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [_share addTarget:self action:@selector(ShareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_showView addSubview:_share];

}

- (void)buttonAction:(UIButton *)button
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    //风火轮
    self.indicator = [[AAA alloc]initWithFrame:CGRectMake(0, 0, 120, 180) superView:self.view];
    [_indicator setMessage:@"努力加载中..."];
    
    if (_indicator.visible==NO) {
        
        [_indicator show:NO];
        
    }else {
        
        [_indicator hide];
        
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//字体视图的收放
- (void)startButton
{
    [self.navigationItem.rightBarButtonItem setEnabled:NO];
    if (self.fontState == 0) {
        [UIView animateWithDuration:0.5 animations:^{
            self.showView.frame = CGRectMake(0, 63, self.view.frame.size.width, self.view.frame.size.height * 0.284);
        } completion:^(BOOL finished) {
            [self.navigationItem.rightBarButtonItem setEnabled:YES];
        }];
        self.fontState = 1;
    } else {
        [UIView animateWithDuration:0.5 animations:^{
            self.showView.frame = CGRectMake(0, -145, self.view.frame.size.width, self.view.frame.size.height * 0.284);
        } completion:^(BOOL finished) {
            [self.navigationItem.rightBarButtonItem setEnabled:YES];
        }];
        self.fontState = 0;
    }
}

//下次进入是否字体大小
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_indicator hide];
    int numOfFont = [[DEFAULTS objectForKey:@"numOfFont"] intValue];
        //改变字体大小
        NSString * str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", numOfFont];
        [self.webView stringByEvaluatingJavaScriptFromString:str];
    
}

//字体放大
- (void)addFontButtonAction
{
    int numOfFont = [[DEFAULTS objectForKey:@"numOfFont"] intValue];
    if (numOfFont < 250) {
        numOfFont += 20;
        NSString * str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", numOfFont];
        [self.webView stringByEvaluatingJavaScriptFromString:str];
        NSString * font = [NSString stringWithFormat:@"%d", numOfFont];
        [DEFAULTS setObject:font forKey:@"numOfFont"];
    }
    
    
}
//字体缩小
- (void)reduceFontButtonAction
{
    int numOfFont = [[DEFAULTS objectForKey:@"numOfFont"] intValue];
    if (numOfFont > 60) {
        numOfFont -=20;
        NSString * str = [NSString stringWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", numOfFont];
        [self.webView stringByEvaluatingJavaScriptFromString:str];
        NSString * font = [NSString stringWithFormat:@"%d", numOfFont];
        [DEFAULTS setObject:font forKey:@"numOfFont"];
    }

}

// 点击收藏的方法
- (void)ButtonAction:(UIButton *)button
{
//    WebModel *web = [WebModel webWithmyId:_myid Image:_dataImaUrl];
//    NSString * str11 = [WebData insertIntoDouberWithModel:web];
//        if ([str11 isEqualToString:@"收藏成功"]) {
//
//            [self.favorite setBackgroundImage:[UIImage imageNamed:@"yishoucang"] forState:UIControlStateNormal];
//            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:str11 delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
//            [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(dimissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alertView, @"alert" ,nil] repeats:NO];
//            [alertView show];
//        
//    }else {
//        [self.favorite setBackgroundImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
//            [WebData deleteFromdouberWithModel:web];
//            UIAlertView *alertView1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"取消收藏" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
//            [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(dimissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alertView1, @"alert1" ,nil] repeats:NO];
//            [alertView1 show];
//            
//        }
}

- (void)dimissAlert:(NSTimer *)alert {
    UIAlertView * alert1 = [alert.userInfo objectForKey:@"alert"];
    UIAlertView * alert2 = [alert.userInfo objectForKey:@"alert1"];
    UIAlertView * alert3 = [alert.userInfo objectForKey:@"alert2"];
    
    [alert1 dismissWithClickedButtonIndex:0 animated:YES];
    [alert2 dismissWithClickedButtonIndex:0 animated:YES];
    [alert3 dismissWithClickedButtonIndex:0 animated:YES];
        
}


// 分享功能
- (void)ShareButtonAction:(UIButton *)button
{
//    self.shareUrl = [NSString stringWithFormat:@"http://www.yueduziji.com/v/%@\n",_myid];
//    SingDanli * danli = [SingDanli shareInstance];
//    NSString *imagePaths = [[NSBundle mainBundle] pathForResource:@"ShareSDK" ofType:@"png"];
    
    //构造分享内容
//    id<ISSContent> publishC = [ShareSDK content:_shareUrl
//                                 defaultContent:@"测试一下"
//                                          image:[ShareSDK imageWithPath:imagePaths]
//                                          title:@"ShareSDK"
//                                            url:@"http://www.mob.com"
//                                    description:@"这是一条测试信息"
//                                      mediaType:SSPublishContentMediaTypeNews];
//    
//    UIView * sender = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.height - 120, 25, self.view.frame.size.width - 50, self.view.frame.size.height * 0.189)];
//    UIImageView * view = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.height - 120, 25, 50, 50)];
//    view.image = [UIImage imageNamed:@"qq.png"];
//    [sender addSubview:view];
//    [self.view addSubview:sender];
//    //创建弹出菜单容器
//    id<ISSContainer> container = [ShareSDK container];
//    [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
//    
//    //弹出分享菜单
//    [ShareSDK showShareActionSheet:nil
//                         shareList:nil
//                           content:publishC
//                     statusBarTips:YES
//                       authOptions:nil
//                      shareOptions:nil
//                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
//                                
//                                if (state == SSResponseStateSuccess)
//                                {
//                                    danli.logiend = YES;
//                                    UIAlertView * view = [[UIAlertView alloc]initWithTitle:@"分享成功" message:@"以及自动返回" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
//                                    [view show];
//                                    [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(dimissAlert:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:view, @"alert2" ,nil] repeats:NO];
//                                }
//                                else if (state == SSResponseStateFail)
//                                {
//                                    NSLog(NSLocalizedString(@"TEXT_ShARE_FAI", @"分享失败,错误码:%d,错误描述:%@"), [error errorCode], [error errorDescription]);
//                                }
//                            }];
}




@end
